package com.wangzeqi.neihanduanzi.discovery;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.web.WebViewActivity;
import com.wangzeqi.neihanduanzi.cache.AllLruCacheLoad;
import com.wangzeqi.neihanduanzi.entity.AllEntity;
import com.wangzeqi.neihanduanzi.entity.CategoryListEntity;

/**
 * Created by dllo on 16/1/19.
 */
public class DiscoveryAdapter extends BaseAdapter {

    private Context context;
    private AllEntity allEntity;
    private RequestQueue queue;

    public DiscoveryAdapter(Context context ) {
        this.context = context;
    }
    public void addData(AllEntity allEntity){
        this.allEntity = allEntity;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (allEntity!=null){
        return allEntity.getData().getCategories().getCategory_list().size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return allEntity.getData().getCategories().getCategory_list().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.discovery_item, null);
            holder = new ViewHolder();
            holder.item = (RelativeLayout) convertView.findViewById(R.id.discovery_item);
            holder.imageView = (NetworkImageView) convertView.findViewById(R.id.discovery_picture);
            holder.textTitle = (TextView) convertView.findViewById(R.id.discovery_content_top);
            holder.textContent = (TextView) convertView.findViewById(R.id.discovery_content);
            holder.textNumLeft = (TextView) convertView.findViewById(R.id.discovery_sub);
            holder.textRight = (TextView) convertView.findViewById(R.id.essence_date);
            holder.btn = (Button) convertView.findViewById(R.id.discovery_dy);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CategoryListEntity entity = allEntity.getData().getCategories().getCategory_list().get(position);

        if (entity != null) {
            queue = Volley.newRequestQueue(context);
            AllLruCacheLoad cache = new AllLruCacheLoad();
            ImageLoader imageloader = new ImageLoader(queue, cache);
            ViewHolder viewHolder = holder;
            holder.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            holder.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            holder.imageView.setImageUrl(entity.getIcon_url(), imageloader);
            holder.textTitle.setText(entity.getName());
            holder.textContent.setText(entity.getIntro());
            holder.textNumLeft.setText(entity.getSubscribe_count());
            holder.textRight.setText(entity.getTotal_updates());
            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "订阅成功", Toast.LENGTH_SHORT).show();
                }
            });
            holder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url", allEntity.getData().getCategories().getCategory_list().get(position).getShare_url());
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {

        private NetworkImageView imageView;
        private TextView textTitle, textContent, textNumLeft, textRight;
        private Button btn;

        private RelativeLayout item;
    }
}